"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var path_1 = require("path");
var webpack_1 = require("webpack");
var clean_webpack_plugin_1 = require("clean-webpack-plugin");
var copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
var css_minimizer_webpack_plugin_1 = __importDefault(require("css-minimizer-webpack-plugin"));
var html_webpack_plugin_1 = __importDefault(require("html-webpack-plugin"));
var mini_css_extract_plugin_1 = __importDefault(require("mini-css-extract-plugin"));
var terser_webpack_plugin_1 = __importDefault(require("terser-webpack-plugin"));
var webpackbar_1 = __importDefault(require("webpackbar"));
var clientPlugin_1 = __importDefault(require("./clientPlugin"));
var paths_1 = require("./paths");
var config = function (commandName, env, css) {
    if (env === void 0) { env = {
        NODE_ENV: process.env.NODE_ENV || "",
    }; }
    if (css === void 0) { css = {}; }
    var isServe = commandName === "serve";
    var isHot = env.NODE_ENV === "development" ||
        env.WEBPACK_WATCH ||
        env.WEBPACK_SERVE ||
        isServe;
    var isBuild = env.NODE_ENV === "production" || env.WEBPACK_BUILD || !isServe;
    var isDevSourcemap = !!(css && css.devSourcemap);
    var cssLoader = [
        isHot && require.resolve("style-loader"),
        isBuild && {
            loader: mini_css_extract_plugin_1.default.loader,
            options: {
                esModule: false,
            },
        },
        {
            loader: require.resolve("css-loader"),
            options: {
                modules: {
                    auto: /\.module\.\w+$/i,
                    localIdentName: "_[local]_[hash:base64:8]",
                    exportLocalsConvention: "camelCase",
                },
                sourceMap: isHot || isDevSourcemap,
            },
        },
        {
            loader: require.resolve("postcss-loader"),
            options: {
                sourceMap: isHot || isDevSourcemap,
                postcssOptions: {
                    plugins: [
                        require.resolve("postcss-flexbugs-fixes"),
                        [
                            require.resolve("postcss-preset-env"),
                            {
                                autoprefixer: {
                                    flexbox: "no-2009",
                                },
                                stage: 3,
                            },
                        ],
                        require.resolve("postcss-normalize"),
                    ],
                },
            },
        },
    ];
    return {
        devServer: {
            hot: true,
            compress: true,
            devMiddleware: {
                publicPath: "/",
            },
            historyApiFallback: {
                disableDotRule: true,
            },
            allowedHosts: "all",
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "*",
                "Access-Control-Allow-Headers": "*",
            },
            static: {
                directory: (0, path_1.resolve)("public"),
            },
            client: {
                progress: false
            }
        },
        infrastructureLogging: {
            level: 'warn'
        },
        entry: (0, paths_1.getEntry)(),
        output: {
            path: (0, path_1.resolve)("dist"),
            publicPath: "/",
            filename: "assets/[name].[chunkhash:8].js",
            chunkFilename: "assets/[name].[chunkhash:8].js",
            assetModuleFilename: "assets/[name].[contenthash:8].[ext]",
            pathinfo: false,
        },
        mode: isHot ? "development" : "production",
        stats: {
            colors: true,
            chunks: false,
            chunkModules: false,
            chunkOrigins: false,
            modules: false,
            moduleTrace: false,
            source: false,
            warnings: true,
            // 打包后的列表
            assets: isBuild,
            entrypoints: isBuild,
            children: false,
        },
        resolve: {
            modules: ["node_modules"],
            extensions: [
                ".js",
                ".jsx",
                ".ts",
                ".tsx",
                ".json",
                ".less",
                ".css",
                ".mjs",
                ".svelte",
                ".vue",
                ".gql",
                ".graphql",
            ],
            alias: (0, paths_1.getAliases)(),
        },
        module: {
            rules: [
                {
                    test: /\.js$/,
                    use: [
                        {
                            loader: "esbuild-loader",
                            options: {
                                loader: "jsx",
                                target: "es2015",
                            },
                        },
                    ],
                    exclude: /node_modules/,
                },
                {
                    test: /\.ts$/,
                    use: [
                        {
                            loader: "esbuild-loader",
                            options: {
                                loader: "ts",
                            },
                        },
                    ],
                    exclude: /node_modules/,
                },
                {
                    test: /\.tsx?$/,
                    use: [
                        {
                            loader: "esbuild-loader",
                            options: {
                                loader: "tsx",
                                target: "es2015",
                            },
                        },
                    ],
                    exclude: /node_modules/,
                },
                {
                    test: /\.(woff|woff2|eot|ttf|otf)$/,
                    type: "asset/resource",
                },
                {
                    test: /\.(png|svg|jpg|jpeg|gif)$/,
                    type: "asset/resource",
                },
                {
                    test: /\.css$/,
                    use: cssLoader.filter(Boolean),
                },
                {
                    test: /\.less$/,
                    use: __spreadArray(__spreadArray([], __read(cssLoader), false), [
                        {
                            loader: "less-loader",
                            options: __assign({ sourceMap: isHot || isDevSourcemap, lessOptions: {
                                    javascriptEnabled: true,
                                }, implementation: require.resolve("less") }, ((css &&
                                css.preprocessorOptions &&
                                css.preprocessorOptions.less) ||
                                {})),
                        },
                    ], false).filter(Boolean),
                },
                {
                    test: /\.s[ac]ss$/i,
                    use: __spreadArray(__spreadArray([], __read(cssLoader), false), [
                        {
                            loader: "sass-loader",
                            options: __assign({ implementation: require.resolve("sass"), sassOptions: {
                                    includePaths: ["node_modules"],
                                }, sourceMap: isHot || isDevSourcemap }, ((css &&
                                css.preprocessorOptions &&
                                css.preprocessorOptions.scss) ||
                                {})),
                        },
                    ], false).filter(Boolean),
                },
            ].filter(Boolean),
        },
        plugins: [
            new webpack_1.DefinePlugin({
                "process.env": JSON.stringify(process.env),
            }),
            new webpack_1.IgnorePlugin({
                resourceRegExp: /^\.\/locale$/,
                contextRegExp: /moment$/,
            }),
            new html_webpack_plugin_1.default({
                meta: false,
                inject: true,
                template: (0, paths_1.getHtmlTemplate)(),
                minify: isHot
                    ? false
                    : {
                        removeComments: true,
                        collapseWhitespace: true,
                        removeRedundantAttributes: true,
                        useShortDoctype: true,
                        removeEmptyAttributes: true,
                        removeStyleLinkTypeAttributes: true,
                        keepClosingSlash: true,
                        minifyJS: true,
                        minifyCSS: true,
                        minifyURLs: true,
                    },
            }),
            !isBuild && new webpackbar_1.default({
                name: 'Client',
            }),
            !isBuild && new clientPlugin_1.default(),
            isBuild && new clean_webpack_plugin_1.CleanWebpackPlugin(),
            isBuild &&
                new copy_webpack_plugin_1.default({
                    patterns: [
                        {
                            from: "./public",
                            globOptions: {
                                ignore: ["**/index.html"],
                            },
                        },
                    ],
                }),
            isBuild &&
                new mini_css_extract_plugin_1.default({
                    filename: "assets/[name].[contenthash:8].css",
                    chunkFilename: "assets/[name].[contenthash:8].css",
                    ignoreOrder: true,
                }),
        ].filter(Boolean),
        optimization: {
            nodeEnv: process.env.NODE_ENV,
            runtimeChunk: true,
            minimize: isBuild,
            minimizer: isBuild
                ? [
                    new terser_webpack_plugin_1.default({
                        terserOptions: {
                            compress: {
                                ecma: 5,
                                comparisons: false,
                                inline: 2,
                            },
                            mangle: {
                                safari10: true,
                            },
                            output: {
                                ecma: 5,
                                comments: false,
                                ascii_only: true,
                            },
                        },
                        extractComments: false,
                    }),
                    new css_minimizer_webpack_plugin_1.default(),
                ]
                : [],
            splitChunks: isBuild
                ? {
                    chunks: "all",
                    cacheGroups: {
                        defaultVendors: {
                            test: /[\\/]node_modules[\\/]/,
                            priority: -10,
                            chunks: "all",
                        },
                        default: {
                            minChunks: 2,
                            chunks: "all",
                            priority: -20,
                            reuseExistingChunk: true,
                        },
                    },
                }
                : false,
        },
        performance: {
            hints: false,
        },
        target: "web",
    };
};
exports.default = config;
